<!--  -->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="permissionList" row-key="id">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template #default="{row}">
            <el-button v-if="row.id" type="text" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="deletePermission(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="`${formData.id?'编辑':'新增'}权限点`" :visible="isShowDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, deletePermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      permissionList: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '0', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: 1
      },
      rules: {
        name: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {

    addPermission(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    async getPermissionList() {
      this.permissionList = listToTreeData(await getPermissionList(), '0')
      console.log(this.permissionList)
    },
    async deletePermission(id) {
      // 二次询问
      await this.$confirm('你确定要删除嘛')
      // 发送请求
      await deletePermission(id)
      // 用户提示
      this.$message.success('删除成功')
      // 更新页面
      this.getPermissionList()
    },
    // 点击按钮事件
    async btnOK() {
      // 校验表单(注意validate返回的是一个promise,所以要await)
      await this.$refs.perForm.validate()
      // 发送请求
      if (!this.formData.id) {
        await addPermission(this.formData)
      } else {
        await updatePermission(this.formData)
      }
      // 用户提示
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 更新页面
      this.getPermissionList()
    },
    // 编辑事件
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.isShowDialog = true
    },
    // 取消事件
    btnCancel() {
      // 清空数据
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: 1
      }
      // 清空校验
      this.$refs.perForm.resetFields()
      // 关闭弹窗
      this.isShowDialog = false
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
